package com.example.demo1122.service.impl;

import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.example.demo1122.entity.Question;
import com.example.demo1122.mapper.QuestionMapper;
import io.reactivex.Flowable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AsyncService {
    @Resource
    private QuestionMapper questionMapper;

    @Async
    public void logAnswer(Question question, Flowable<GenerationResult> result){
        StringBuilder fullContent = new StringBuilder();
        // blockingForEach,在每一次响应后block获取输出append
        result.blockingForEach(message -> {
            fullContent.append(message.getOutput().getChoices().get(0).getMessage().getContent());
        });
        question.setAnswerContent(fullContent.toString());
        questionMapper.insert(question);
    }
}
